package cn.wegoteam.shop.po;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import cn.wegoteam.shop.cache.StaticDataCache;
import cn.wegoteam.shop.enu.UserType;
import cn.wegoteam.shop.util.Const;
import cn.wegoteam.shop.util.GetReqRes;
import cn.wegoteam.shop.util.MyString;

/**
 * Product entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "product", catalog = "shop")
public class Product extends BasePo implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 482997323353772907L;
	private Integer id;
	private Integer sellNum;//销售量
	private Integer remainder;//剩余量
	private String imgUrl;
	private String type;//分类
	private Float price;
	private Float marketPrice;//市场价
	private Integer click;
	private Integer scores;//返还积分
	private String content;//产品详情
	private String message;//商品简介
	private Float discount;
	private String brand;//品牌
	private String name;
	private Float pifaPrice;//批发价格
	private Integer flag;
	private Integer favorite;//收藏商品
	private String attr;//商品属性
	private Integer commentNum;//评论量
	private String indexImg;//封面图片，不映射至表中
	private String[] imgs;//所有图片地址，不映射至表中
	private HashMap<String,ArrayList<Staticdata>> attrMaps;//商品属性
	// Property accessors
	public Product(){}
	public Product(String id){
		try{
			this.id = Integer.parseInt(id);
		}catch(Exception e){
			this.id = 0;
			e.printStackTrace();
		}
	}
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "sellNum", nullable = false, insertable = false)
	public Integer getSellNum() {
		return this.sellNum;
	}

	public void setSellNum(Integer sellNum) {
		this.sellNum = sellNum;
	}
	
	@Column(name = "favorite", nullable = false, insertable = false)
	public Integer getFavorite() {
		return this.favorite;
	}

	public void setFavorite(Integer favorite) {
		this.favorite = favorite;
	}
	

	@Column(name = "remainder", nullable = false)
	public Integer getRemainder() {
		return this.remainder;
	}

	public void setRemainder(Integer remainder) {
		this.remainder = remainder;
	}
	@Column(name = "commentNum", nullable = false, insertable = false)
	public Integer getCommentNum() {
		return this.commentNum;
	}

	public void setCommentNum(Integer commentNum) {
		this.commentNum = commentNum;
	}
	
	@Column(name = "imgUrl", length = 400)
	public String getImgUrl() {
		return this.imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	@Column(name = "type", nullable = false, length = 15)
	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Column(name = "price", nullable = false, precision = 0, scale = 0)
	public Float getPrice() {
		return this.price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}
	@Column(name = "marketPrice", nullable = false, precision = 0, scale = 0)
	public Float getMarketPrice() {
		return this.marketPrice;
	}

	public void setMarketPrice(Float marketPrice) {
		this.marketPrice = marketPrice;
	}
	
	@Column(name = "click", nullable = false ,insertable=false)
	public Integer getClick() {
		return this.click;
	}

	public void setClick(Integer click) {
		this.click = click;
	}

	@Column(name = "scores", nullable = false)
	public Integer getScores() {
		return this.scores;
	}

	public void setScores(Integer scores) {
		this.scores = scores;
	}

	@Column(name = "content", length = 65535)
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name = "message", length = 400)
	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Column(name = "discount", precision = 0, scale = 0)
	public Float getDiscount() {
		return this.discount;
	}

	public void setDiscount(Float discount) {
		this.discount = discount;
	}

	@Column(name = "brand", nullable = false, length = 15)
	public String getBrand() {
		return this.brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	@Column(name = "name", nullable = false, length = 45)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Column(name = "attr", length = 100)
	public String getAttr() {
		return this.attr;
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}
	
	@Column(name = "pifaPrice", nullable = false, precision = 0, scale = 0)
	public Float getPifaPrice() {
		return this.pifaPrice;
	}

	public void setPifaPrice(Float pifaPrice) {
		this.pifaPrice = pifaPrice;
	}

	@Column(name = "flag", nullable = false ,insertable=false)
	public Integer getFlag() {
		return this.flag;
	}
	public void setFlag(Integer flag) {
		this.flag = flag;
	}
	@Transient
	public String getIndexImg() {
		if(indexImg==null)
			this.indexImg =(imgUrl!=null&&imgUrl.indexOf(",")>0)?imgUrl.split(",")[0]:imgUrl;
		return indexImg;
	}
	@Transient
	public String[] getImgs() {
		if(imgs==null)
			this.imgs =(imgUrl!=null&&imgUrl.indexOf(",")>0)?imgUrl.split(","):new String[]{imgUrl};
		return imgs;
	}
	/**
	 * detail页面输出商品类型
	 * @return
	 */
	@Transient
	public HashMap<String,ArrayList<Staticdata>> getAttrMaps(){
		if(attrMaps==null&&!MyString.isEmpty(attr)){
			attrMaps = new HashMap<String,ArrayList<Staticdata>>();
			String[] attrs = attr.split(",");
			for(String at:attrs){
				Staticdata ps = StaticDataCache.getPStaticdata(at);
				if(MyString.isEmpty(ps.getCode())) continue;
				if(attrMaps.containsKey(ps.getName())){
					attrMaps.get(ps.getName()).add(StaticDataCache.getStaticdata(at));
				}else{
					ArrayList<Staticdata> als = new ArrayList<Staticdata>();
					als.add(StaticDataCache.getStaticdata(at));
					attrMaps.put(ps.getName(), als);
				}
			}
		}
		return attrMaps;
	}
	@Transient
	public String getPattrs(){
		if(attr==null)
			return "";
			String pattrs = "";
			String[] attrs = attr.split(",");
			for(String at:attrs){
				Staticdata ps = StaticDataCache.getPStaticdata(at);
				if(MyString.isEmpty(ps.getCode())) continue;
				if(!pattrs.contains(ps.getCode()+","))
					pattrs+=ps.getCode()+",";
			}
		return pattrs;
	}
	@Transient
	public Float getTruePrice(){
		User user = (User) GetReqRes.getSession().get(Const.SESS_USER);
		if(user!=null&&user.getType().equals(UserType.PIFA))
			return this.pifaPrice;
		else
			return this.price;
	}
	@Transient
	public String getShowPifaPrice(){
		User user = (User) GetReqRes.getSession().get(Const.SESS_USER);
		if(user!=null&&user.getType().equals(UserType.PIFA))
			return " 批发价:￥  "+this.pifaPrice;
		else
			return "";
	}
}